/*! \file HXPlayer.h
    \brief Head X application entry point; handles window messages.

	Head X | Flinders University | Martin Luerssen & Trent Lewis | 2009+
*/

#pragma once

#define HEADX_VERSION 87
#define MAX_TEXT_LENGTH 4096

#include "resource.h"
#include "stdafx.h"
using namespace std;

#include "HXDisplay.h"

//-----------------------------------------------------------------------------

//! Main application variables.
struct HXMain
{
	HXMain();
	void Destroy();

	HINSTANCE hInst;						// current instance
	TCHAR szTitle[MAX_LOADSTRING];			// title bar text
	TCHAR szWindowClass[MAX_LOADSTRING];	// the main window class name

	HANDLE hAuxThread;						// handle to current support thread (mainly used for debugging)
	HANDLE hSpeechThread;					// handle to speech thread (for parsing and speaking text)
	bool doAuxExit;							// if true, support thread will exit as quickly as possible
	bool doSpeechExit;						// same for speech thread
	bool bSpeakNow;							// if true, process what's in the current command string
	bool bBlock;							// if true, do not process next command string until this one is done
	HANDLE hSpeechMutex;					// speech thread mutex
	wstring commandString;					// text to speak/command to process
};

//-----------------------------------------------------------------------------

//! Working paths of the application.
class HXMainPaths
{
public:
	wchar_t* GetStartupPath();
	wchar_t* GetStartupFile();
	wchar_t* GetLastFilePath();
	wchar_t* GetLastFileName();
	void	 ResetLastFile();
	void	 SetWorkingPath(const wstring& path);
	bool	 SplitFilePath(wstring fullArg, wstring* pFileDir = NULL, wstring* pFileName = NULL, bool bSplitOnly = false);
	void	 SetLast(wstring& fileDir, wstring& fileName);

private:
	wchar_t m_startupPath[MAX_PATH];	 // stores working directory at program start
	wchar_t m_startupFile[MAX_PATH];     // stores filename specified on command-line
	wchar_t m_lastFilePath[MAX_PATH];	 // stores directory of last accessed file
	wchar_t m_lastFileName[MAX_PATH];	 // stores name of last accessed file
};

//-----------------------------------------------------------------------------

//! Different interface modes for rotating the head with a mouse.
enum HXMouseMode
{
	HXMM_LOCK,
	HXMM_CAMERA,
	HXMM_LIGHT,
	HXMM_SUBTITLE,
	HXMM_WINDOW
};

//-----------------------------------------------------------------------------

//! Mouse input state.
struct HXMouse
{
	HXMouse() : clicked(false), headMouseMode(HXMM_CAMERA) {}
	int oldX;
	int oldY;
	int startX;
	int startY;
	bool clicked;
	HXMouseMode headMouseMode;
};

//-----------------------------------------------------------------------------

ATOM MyRegisterClass(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void	ExitProc();

// message callbacks
void	OnDestroy(HWND hwnd);
void	OnSize(HWND hwnd, UINT state, int cx, int cy);
void	OnKeyDown(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags);
void	OnKeyUp(HWND hwnd, UINT vk, BOOL fDown, int cRepeat, UINT flags);
void	OnKeyPressed(HWND hwnd, unsigned short charcode, int keydata);
void	OnLButtonDown(HWND hwnd, BOOL dblClk, int x, int y, UINT flag);
void	OnRButtonDown(HWND hwnd, BOOL dblClk, int x, int y, UINT flag);
void	OnMButtonDown(HWND hwnd, BOOL dblClk, int x, int y, UINT flag);
void	OnLButtonUp(HWND hwnd,int x,int y,UINT keyFlags);
void	OnRButtonUp(HWND hwnd,int x,int y,UINT keyFlags);
void	OnMButtonUp(HWND hwnd,int x,int y,UINT keyFlags);
void	OnMouseMove(HWND hwnd, int x, int y, UINT keyFlags);

// auxiliary thread (for testing)
void	RunSimpleThread(int menu_id);
static DWORD WINAPI SimpleThread(LPVOID lpParam);

// speech/command processing thread
DWORD WINAPI RunSpeechThread(LPVOID lpParam);
void	AsyncSpeak(const wstring& text, bool block = false);
void	SyncSpeak(const wstring& text);
void	SyncSpeak(const wstring& text, HXDisplay* pDisplay);

//=============================================================================

extern HXMain g_main;
extern HXMainPaths g_paths;
extern HXMouse g_mouse;
